package net.xiaoyu233.mitemod.miteite.inventory.container;

import net.minecraft.*;
import net.xiaoyu233.mitemod.miteite.tileentity.TileEntityRecyclingTable;

public class ContainerRecyclingTable extends Container   {
    private TileEntityRecyclingTable recyclingTable;
    private int recyclingProgress;
    private int lastRecyclingProgress;
    private int currentRecyclingTime;
    private int lastCurrentRecyclingTime;
    private boolean hasLava;
    private boolean lastHasLava;
    private int tableLevel;
    private int lastTableLevel;

    public ContainerRecyclingTable(EntityPlayer player, TileEntityRecyclingTable tileEntity) {
        super(player);
        this.recyclingTable = tileEntity;
        this.recyclingProgress = 0;
        this.lastRecyclingProgress = 0;
        this.currentRecyclingTime = 0;
        this.lastCurrentRecyclingTime = 0;
        this.hasLava = recyclingTable.hasLava();
        this.lastHasLava = this.hasLava;
        this.tableLevel = recyclingTable.getTableLevel();
        this.lastTableLevel = this.tableLevel;

        // 输入槽 (2个)
        this.addSlotToContainer(new Slot(tileEntity, 0, 56, 17));
        this.addSlotToContainer(new Slot(tileEntity, 1, 56, 53));

        // 输出槽 (2个，不可放置物品)
        this.addSlotToContainer(new Slot(tileEntity, 2, 113, 17) {
            @Override
            public boolean isItemValid(ItemStack stack) {
                return false;
            }
        });
        this.addSlotToContainer(new Slot(tileEntity, 3, 113, 53) {
            @Override
            public boolean isItemValid(ItemStack stack) {
                return false;
            }
        });

        // 玩家背包
        for (int row = 0; row < 3; ++row) {
            for (int col = 0; col < 9; ++col) {
                this.addSlotToContainer(new Slot(player.inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
            }
        }

        // 玩家快捷栏
        for (int col = 0; col < 9; ++col) {
            this.addSlotToContainer(new Slot(player.inventory, col, 8 + col * 18, 142));
        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer player) {
        return recyclingTable.isUseableByPlayer(player);
    }

    @Override
    public void onContainerClosed(EntityPlayer par1EntityPlayer) {
        super.onContainerClosed(par1EntityPlayer);
        if (!this.world.isRemote && recyclingTable != null) {
            this.recyclingTable.closeChest();
        }
    }

    @Override
    public void detectAndSendChanges() {
        super.detectAndSendChanges();

        // 获取最新的进度值
        this.recyclingProgress = recyclingTable.getRecyclingProgress();
        this.currentRecyclingTime = recyclingTable.getCurrentRecyclingTime();
        this.hasLava = recyclingTable.hasLava();
        this.tableLevel = recyclingTable.getTableLevel();

        // 检查进度是否有变化，如果有则发送更新
        for (Object crafter : this.crafters) {
            ICrafting crafting = (ICrafting) crafter;

            if (this.lastRecyclingProgress != this.recyclingProgress) {
                crafting.sendProgressBarUpdate(this, 0, this.recyclingProgress);
            }
            if (this.lastCurrentRecyclingTime != this.currentRecyclingTime) {
                crafting.sendProgressBarUpdate(this, 1, this.currentRecyclingTime);
            }
            if (this.lastHasLava != this.hasLava) {
                crafting.sendProgressBarUpdate(this, 2, this.hasLava ? 1 : 0);
            }
            if (this.lastTableLevel != this.tableLevel) {
                crafting.sendProgressBarUpdate(this, 3, this.tableLevel);
            }
        }

        // 更新最后的值
        this.lastRecyclingProgress = this.recyclingProgress;
        this.lastCurrentRecyclingTime = this.currentRecyclingTime;
        this.lastHasLava = this.hasLava;
        this.lastTableLevel = this.tableLevel;
    }

    @Override
    public void onCraftGuiOpened(ICrafting crafting) {
        super.onCraftGuiOpened(crafting);
        // 发送当前进度给新打开的GUI
        crafting.sendProgressBarUpdate(this, 0, recyclingTable.getRecyclingProgress());
        crafting.sendProgressBarUpdate(this, 1, recyclingTable.getCurrentRecyclingTime());
        crafting.sendProgressBarUpdate(this, 2, recyclingTable.hasLava() ? 1 : 0);
        crafting.sendProgressBarUpdate(this, 3, recyclingTable.getTableLevel());
    }

    @Override//updateProgressBar
    public void b(int id, int value) {
        if (id == 0) {
            recyclingTable.setRecyclingProgress(value);
        } else if (id == 1) {
            recyclingTable.setCurrentRecyclingTime(value);
        } else if (id == 2) {
            recyclingTable.setHasLava(value == 1);
        } else if (id == 3) {
            recyclingTable.setTableLevel(value);
        }
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) {
        ItemStack stack = null;
        Slot slot = (Slot) inventorySlots.get(slotIndex);

        if (slot != null && slot.getHasStack()) {
            ItemStack stackInSlot = slot.getStack();
            stack = stackInSlot.copy();

            if (slotIndex >= 2 && slotIndex < 4) {
                // 从输出槽移动到玩家库存
                if (!this.mergeItemStack(stackInSlot, 4, 40, true)) {
                    return null;
                }
                slot.onSlotChange(stackInSlot, stack);
            } else if (slotIndex >= 4) {
                // 从玩家库存移动到输入槽
                if (recyclingTable.isItemValidForSlot(0, stackInSlot)) {
                    // 尝试放入第一个输入槽
                    if (!this.mergeItemStack(stackInSlot, 0, 1, false)) {
                        // 第一个输入槽已满，尝试第二个输入槽
                        if (!this.mergeItemStack(stackInSlot, 1, 2, false)) {
                            return null;
                        }
                    }
                } else if (slotIndex >= 4 && slotIndex < 31) {
                    // 从主背包移动到快捷栏
                    if (!this.mergeItemStack(stackInSlot, 31, 40, false)) {
                        return null;
                    }
                } else if (slotIndex >= 31 && slotIndex < 40) {
                    // 从快捷栏移动到主背包
                    if (!this.mergeItemStack(stackInSlot, 4, 31, false)) {
                        return null;
                    }
                }
            } else if (!this.mergeItemStack(stackInSlot, 4, 40, false)) {
                return null;
            }

            if (stackInSlot.stackSize == 0) {
                slot.putStack(null);
            } else {
                slot.onSlotChanged();
            }

            if (stackInSlot.stackSize == stack.stackSize) {
                return null;
            }

            slot.onPickupFromSlot(player, stackInSlot);
        }

        return stack;
    }
}